package com.danskin.wechat.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.danskin.wechat.api.client.DanskinCRMApiClient;
import com.danskin.wechat.constant.DanskinConstants;
import com.danskin.wechat.dao.CustInfoExDao;
import com.danskin.wechat.dao.WxFansCardDao;
import com.danskin.wechat.entity.CustInfoEx;
import com.danskin.wechat.entity.CustInfoExView;
import com.danskin.wechat.entity.example.CustInfoExExample;
import com.danskin.wechat.service.CustInfoExService;
import com.danskin.wechat.util.LogUtil;

@Service
public class CustInfoExServiceImpl implements CustInfoExService {
	@Autowired
	private CustInfoExDao custInfoExDao;
	@Autowired
	private WxFansCardDao fansCadDao;

	@Override
	public CustInfoEx queryCustInfoEx(String appId, String openId) {
		// TODO Auto-generated method stub
		return custInfoExDao.queryCustInfoEx(appId, openId);
	}

	@Override
	public CustInfoEx selectByCustId(int mpId, int custId) {
		// TODO Auto-generated method stub
		return custInfoExDao.selectByCustId(mpId, custId);
	}

	@Override
	public int countByExample(CustInfoExExample example) {
		LogUtil.info("=====查询符合条件的用户扩展信息数据数参数："+ToStringBuilder.reflectionToString(example));
		int selectCount = 0;
		try {
			selectCount = custInfoExDao.countByExample(example);
		} catch (Exception e) {
			LogUtil.error("=======查询符合条件的用户扩展信息数据数异常", e);
		}
		return selectCount;
	}
	
	@Override
	public int selectCountByExample(CustInfoEx param) {
		LogUtil.info("=======查询符合条件的用户扩展信息数据数参数："+ToStringBuilder.reflectionToString(param));
		int selectCount = 0;
		try {
			selectCount = custInfoExDao.selectCountByExample(param);
		} catch (Exception e) {
			LogUtil.error("=======查询符合条件的用户扩展信息数据数异常", e);
		}
		return selectCount;
	}
	
	@Override
	public int insertSelective(CustInfoEx entity) {
		LogUtil.info("=======新建用户扩展信息参数："+ToStringBuilder.reflectionToString(entity));
		int custInfoExId = 0;
		try {
			custInfoExDao.insertSelective(entity);
			if(entity.getId() != null) {
				custInfoExId = entity.getId();
			}
		} catch (Exception e) {
			LogUtil.error("========新建用户扩展信息异常", e);
		}
		return custInfoExId;
	}

	@Override
	public List<CustInfoEx> selectCustInfoExList(CustInfoEx custInfoEx) {
		LogUtil.info("======查询符合条件的用户扩展信息参数：" + ToStringBuilder.reflectionToString(custInfoEx));

		List<CustInfoEx> custInfoExList = new ArrayList<CustInfoEx>();

		try {
			custInfoExList = custInfoExDao.selectCustInfoExList(custInfoEx);
		} catch (Exception e) {
			LogUtil.error("=======查询符合条件的用户扩展信息异常", e);
		}
		return custInfoExList;
	}





	@Override
	public int updateCustInfoEx(CustInfoEx custInfoEx) {
		LogUtil.info("======更新用户扩展信息参数：" + ToStringBuilder.reflectionToString(custInfoEx));

		int updateCount = 0;

		try {
			updateCount = custInfoExDao.updateCustInfoEx(custInfoEx);
		} catch (Exception e) {
			LogUtil.error("======更新用户扩展信息异常", e);
		}
		return updateCount;
	}



	@Override
	public CustInfoExView getCustInfoEx(String brandId, String posCardNo) {
		JSONObject json = DanskinCRMApiClient.createInstance().getMemberIdByPosCardNo(brandId, posCardNo);
		// 正确返回示例：
		// {"status":1,"errorMsg":"","memberId":"bcfa4869729c4e4ca63986eaf9ffebe0","brand":"1"}
		if (json != null && json.containsKey("status") && json.getInteger("status").equals(1)) {
			String memberId = json.getString("memberId");
			return custInfoExDao.selectByMemberId(brandId, memberId);

		}
		return null;

	}

	@Override
	public boolean getCustActiveMemberCard(String appId, String openId) {
		Integer count = fansCadDao.countByOpenIdActived(appId, openId, DanskinConstants.WX_CARD_CONFIG_CARD_TYPE_MEMBER_CARD);
		return count > 0;
	}

	@Override
	public CustInfoEx queryCustInfoExByControllerId(String openId) {
		// TODO Auto-generated method stub
		return custInfoExDao.queryCustInfoExById(openId);
	}

	@Override
	public CustInfoEx queryCustInfoExById(String openId) {
		// TODO Auto-generated method stub
		return custInfoExDao.queryCustInfoExById(openId);
	}
}
